package com.tools.scanner.utils

import android.content.Context
import android.content.SharedPreferences
import com.tools.scanner.QrApp

object PreferencesUtil {
    private const val FILE_NAME = "scanner"

    private val mApp = QrApp.app

    fun putValue(key: String, value: Any) {
        val edit = getSharedPreferences().edit()

        when (value) {
            is Int -> {
                edit.putInt(key, value)
            }

            is Float -> {
                edit.putFloat(key, value)
            }

            is String -> {
                edit.putString(key, value)
            }

            is Boolean -> {
                edit.putBoolean(key, value)
            }

            is Long -> {
                edit.putLong(key, value)
            }
        }

        edit.apply()
    }

    fun getIntValue(key: String, def: Int = 0): Int {
        return getSharedPreferences().getInt(key, def)
    }

    fun getFloatValue(key: String, def: Float = 0.0f): Float {
        return getSharedPreferences().getFloat(key, def)
    }

    fun getLongValue(key: String, def: Long = 0): Long {
        return getSharedPreferences().getLong(key, def)
    }

    fun getBooleanValue(key: String, def: Boolean = false): Boolean {
        return getSharedPreferences().getBoolean(key, def)
    }

    fun getStringValue(key: String, def: String = ""): String {
        return getSharedPreferences().getString(key, def)!!
    }

    private fun getSharedPreferences(): SharedPreferences {
        return mApp.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE)
    }
}